<template>
  <div>
    <van-address-edit
      :area-list="areaList"
      show-delete
      :address-info="InitialValue"
      show-set-default
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
    >
      <span class="van-cell__title van-field__label">标签</span>
      <van-radio-group v-model="tag" class="van-cell__value van-field__value">
        <van-radio name="家">家</van-radio>
        <van-radio name="公司">公司</van-radio>
        <van-radio name="学校">学校</van-radio>
      </van-radio-group>
    </van-address-edit>
  </div>
</template>

<script>
import { Toast } from "vant";
import {
  reqAddressList,
  reqAddressListAdd,
  reqAddressEdit,
  reqAddressDel,
} from "../../http/api";
import { areaList } from "@vant/area-data";
// {addressDetail: "qweq"
// areaCode: "110101"
// city: "北京市"
// country: ""
// county: "东城区"
// isDefault: false
// name: "11"
// postalCode: ""
// province: "北京市"
// tel: "19972933753"}
export default {
  mounted() {
    // 查询当前要修改的地址
    if (this.$route.query.id) {
      reqAddressList({
        uid: JSON.parse(localStorage.getItem("user")).uid,
        id: this.$route.query.id,
      }).then((res) => {
        if (res.data.code == 200) {
          let json = res.data.list[0];
          this.InitialValue = {
            name: json.username,
            tel: json.userphone,
            addressDetail: json.useraddress,
            areaCode: json.regioncode,
            isDefault: json.status === 1,
          };
        }
      });
    }
  },
  data() {
    return {
      tag: "家",
      areaList,
      InitialValue: {},
      conent: {},
    };
  },
  methods: {
    onSave(e) {
      if (this.$route.query.id) {
        reqAddressEdit({
          id: this.$route.query.id,
          username: e.name,
          userphone: e.tel,
          location: e.province + e.city + e.county,
          useraddress: e.addressDetail,
          status: e.isDefault ? 1 : 0,
          lable: this.tag,
        }).then((res) => {
          if (res.data.code == 200) {
            Toast(res.data.msg);
            this.$router.push("/address");
          }
        });
      } else {
        // Toast("save");
        reqAddressListAdd({
          uid: JSON.parse(localStorage.getItem("user")).uid,
          username: e.name,
          userphone: e.tel,
          location: e.province + e.city + e.county,
          useraddress: e.addressDetail,
          status: e.isDefault ? 1 : 0,
          regioncode: e.areaCode,
          lable: this.tag,
        }).then((res) => {
          if (res.data.code == 200) {
            this.$router.push("/address");
          }
        });
      }
      // console.log(e);
    },
    onDelete() {
      reqAddressDel({
        id: this.$route.query.id,
      }).then((res) => {
        if (res.data.code == 200) {
          Toast("delete");
          this.$router.replace('/address')
        }
      });
    },
  },
};
</script>

<style scoped>
.van-radio-group {
  overflow: hidden;
  float: right;
}
.van-radio {
  float: left;
}
</style>